<?
class Helper{

	public static function gettime($time){
		$time=strtotime($time);
        $diftime = time() - $time;

        if($diftime < 31536000){
            if($diftime>=86400){
                return round($diftime/86400).' 天前';
            }else if($diftime>=3600){
                return round($diftime/3600).' 小时前';
            }else if($diftime>=60){
                return round($diftime/60).' 分钟前';
            }else{
                return ($diftime+1).' 秒钟前';
            }
        }else{
            return date("Y-m-d H:i:s", $time);
        }
	}

	public static function getuname($text, $filter_name=''){
        preg_match_all('/\B\@([a-zA-Z0-9\x80-\xff]{4,20})/' ,$text, $out, PREG_PATTERN_ORDER);
        $uname = array_unique($out[1]);
        if($filter_name && in_array($filter_name, $uname)){
            foreach($uname as $k=>$v){
                if($v == $filter_name){
                    unset($uname[$k]);
                    break;
                }
            }
        }
        return $uname;
	}

    public static function e($value,$def=false){
        if (isset($value))
            return $value;
        else
            return $def;
    }

    public static function formatcontent($text){
        // $image = '/(http[s]?:\/\/?('.$options['safe_imgdomain'].').+\.(jpg|jpe|jpeg|gif|png))\w*/';
        $image = '/(http[s]?:\/\/?(127\.0\.0\.1\/).+\.(jpg|jpe|jpeg|gif|png))\w*/';
        
        if(preg_match($image, $text)){
                $text = preg_replace($image, '<img src="\1" />', $text);
        }
        // 各大网站的视频地址格式经常变，能识别一些，不能识别了再改。
        // youku
        if(strpos($text, 'player.youku.com')){
            $text = preg_replace('/http:\/\/player.youku.com\/player.php\/sid\/([a-zA-Z0-9\=]+)\/v.swf/', '<embed src="http://player.youku.com/player.php/sid/\1/v.swf" quality="high" width="590" height="492" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash"></embed>', $text);
        }
        
        if(strpos($text, 'v.youku.com')){
            $text = preg_replace('/http:\/\/v.youku.com\/v_show\/id_([a-zA-Z0-9\=]+)(\/|.html?)?/', '<embed src="http://player.youku.com/player.php/sid/\1/v.swf" quality="high" width="590" height="492" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash"></embed>', $text);
        }
        // tudou
        if(strpos($text, 'www.tudou.com')){
            if(strpos($text, 'programs/view')){
                $text = preg_replace('/http:\/\/www.tudou.com\/(programs\/view|listplay)\/([a-zA-Z0-9\=\_\-]+)(\/|.html?)?/', '<embed src="http://www.tudou.com/v/\2/" quality="high" width="638" height="420" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash"></embed>', $text);
            }else if(strpos($text, 'albumplay')){
                $text = preg_replace('/http:\/\/www.tudou.com\/albumplay\/([a-zA-Z0-9\=\_\-]+)\/([a-zA-Z0-9\=\_\-]+)(\/|.html?)?/', '<embed src="http://www.tudou.com/a/\1/" quality="high" width="638" height="420" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash"></embed>', $text);
            }else{
                $text = preg_replace('/http:\/\/www.tudou.com\/(programs\/view|listplay)\/([a-zA-Z0-9\=\_\-]+)(\/|.html?)?/', '<embed src="http://www.tudou.com/l/\2/" quality="high" width="638" height="420" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash"></embed>', $text);
            }
        }
        // qq
        if(strpos($text, 'v.qq.com')){
            if(strpos($text, 'vid=')){
                $text = preg_replace('/http:\/\/v.qq.com\/(.+)vid=([a-zA-Z0-9]{8,})/', '<embed src="http://static.video.qq.com/TPout.swf?vid=\2&auto=0" allowFullScreen="true" quality="high" width="590" height="492" align="middle" allowScriptAccess="always" type="application/x-shockwave-flash"></embed>', $text);
            }else{
                $text = preg_replace('/http:\/\/v.qq.com\/(.+)\/([a-zA-Z0-9]{8,}).(html?)/', '<embed src="http://static.video.qq.com/TPout.swf?vid=\2&auto=0" allowFullScreen="true" quality="high" width="590" height="492" align="middle" allowScriptAccess="always" type="application/x-shockwave-flash"></embed>', $text);
            }
        }


        if(strpos(' '.$text, '@')){
            $text = preg_replace('/\B\@([a-zA-Z0-9\x80-\xff]{4,20})/', '@<a href="javascript:void(0);" onclick=\'replyto("$1");\'>$1</a>', $text);
        }
        // // url
        if(strpos(' '.$text, 'http')){
            $text = ' ' . $text;
            $text = preg_replace(
                '`([^"=\'>])((http|https|ftp)://[^\s<]+[^\s<\.)])`i',
                '$1<a href="$2" target="_blank" rel="nofollow">$2</a>',
                $text
            );
            $text = substr($text, 1);
        }
        
        $text = str_replace("\r\n", '<br/>', $text);
        
        return $text;
    }

    public static function tags($tag){
        return explode("|", $tag);
    }

}
?>